package com.vogelsk.archangelsk.media;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class SoundManager {

	private boolean active;
	private LinkedHashMap<String, Clip> clipMap = new LinkedHashMap<String, Clip>();
	public static final String SHOT_CLIP = "SHOT";
	public static final String HIT_CLIP = "HIT";

	private AudioInputStream inputStream;
	private AudioInputStream inputStream2;

	public void init() {

		try {
			Clip shotClip = AudioSystem.getClip();
			Clip hitClip = AudioSystem.getClip();

			inputStream = AudioSystem.getAudioInputStream(getClass()
					.getResource("/44magnum.wav"));
			inputStream2 = AudioSystem.getAudioInputStream(getClass()
					.getResource("/bazooka.wav"));
			shotClip.open(inputStream);
			hitClip.open(inputStream2);

			clipMap.put(SHOT_CLIP, shotClip);
			clipMap.put(HIT_CLIP, hitClip);
			active  = true;

		} catch (LineUnavailableException e) {
			e.printStackTrace();
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void close() {
		for (Entry<String, Clip> c : clipMap.entrySet()){
			c.getValue().close();
			}
	}

	public void play(String string) {
		Clip clip = clipMap.get(string);
		if (clip != null)
			play(clip);
	}

	public void play(Clip clip) {
		if(active){
		clip.start();
		clip.setFramePosition(0);}
	}

	public void toggle(boolean state){
		active = state;
		
		
	}
	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isActive() {
		return active;
	}
}
